package com.ptszyxx.popose.module.main.dynamic.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.enums.ActionEnum;
import com.ptszyxx.popose.common.utils.YActionEventUtil;
import com.ptszyxx.popose.common.utils.sensitive.YSensitiveUtil;
import com.ysg.http.data.CommonRepository;
import com.ptszyxx.popose.common.oos.YOosUtil;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.utils.YFileUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessResult;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YSPUtils;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.pic.SelectPicBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DynamicReleaseVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> content = new ObservableField<>();

    public DynamicReleaseVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    @Override
    public void requestUploadAction() {
        YActionEventUtil.getInstance().upload(this, model, ActionEnum.dynamic_release);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onSubmitEvent = new SingleLiveEvent<>();
        public SingleLiveEvent onSensitiveEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onSubmitCommand = new BindingCommand(() -> uc.onSubmitEvent.call());

    //=========================================接口：start===========================================

    private boolean check(){
        if (YStringUtil.isEmpty(content.get())){
            YToastUtil.showShort(R.string.dynamic_release_content_hint);
            return false;
        }
        String sensitiveData = YSensitiveUtil.checkSensitiveContent(content.get());
        if (YStringUtil.isNotEmpty(sensitiveData)){
            uc.onSensitiveEvent.call();
            YSensitiveUtil.save(this, model, content.get(), sensitiveData);
            return false;
        }
        return true;
    }

    public void requestOosToken(){
        YOosUtil.getInstance().getOosToken(model, this);
    }

    public void requestUploadPic(List<SelectPicBean> list) {
        if (!check())return;
        showDialog("图片上传中");
        String userId = YSPUtils.getInstance().getUserId();
        YOosUtil.getInstance().upload(list, userId, new YOosUtil.OnOosListener() {
            @Override
            public void onSuccess(String url) {
            }

            @Override
            public void onSuccess(List<String> list) {
                requestDynamicRelease(list);
            }

            @Override
            public void onError() {
            }
        });
    }

    public void requestDynamicRelease(List<String> list) {
        if (!check())return;
        Map<String, String> map = new HashMap<>();
        map.put("message", content.get());

        if (YCollectionUtil.isNotEmpty(list)){
            map.put("media", YStringUtil.clearEndComma(YStringUtil.getIds(list)));
            if (YFileUtil.isVideo(list.get(0))){
                map.put("type", "2");//视频
            }else{
                map.put("type", "1");//图片
            }
        }else{
            map.put("type", "3");
        }

        HttpUtils.getInstance()
                .data(model.dynamicRelease(map), this,
                        new OnSuccessResult() {
                            @Override
                            public void onSuccessResult(BaseResponse response) {
                                YToastUtil.showShort(R.string.success);
                                YActivityUtil.getInstance().close(DynamicReleaseVM.this);
                            }
                        });
    }

}
